package com.zansc.longd.controller;

import com.zansc.longd.domain.entity.s.User;
import com.zansc.longd.model.base.bean.ResponseBean;
import com.zansc.longd.model.base.in.UserIn;
import com.zansc.longd.model.code.ErrorCode;
import com.zansc.longd.service.s.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 添加用户应由管理员权限添加
     *
     * @param userIn
     * @return
     */
    @PostMapping("add")
    public ResponseBean add(UserIn userIn) {
        userService.add(userIn);
        return ResponseBean.success("用户添加成功");
    }
    
    /**
     * 用户登录接口
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("login")
    public ResponseBean login(String username, String password) {
        String token = userService.login(username, password);
        return ResponseBean.success((Object) token, "登录成功！");
    }
    
    /**
     * 测试登录接口
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("testlogin")
    public ResponseBean testlogin() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            return ResponseBean.success(200,"登录成功");
        } else {
            return ResponseBean.error(ErrorCode.SIGNATURE_NOT_MATCH);
        }
    }
    
    /**
     * 获取用户列表
     *
     * @return
     */
    @GetMapping("findAll")
    public ResponseBean findAll() {
        List<User> all = userService.findAll();
        return ResponseBean.success(all);
    }
    
    @GetMapping("selectByPrimaryKey")
    public ResponseBean selectByPrimaryKey(int id) {
        User one = userService.selectByPrimaryKey(id);
        return ResponseBean.success(one);
    }
    
    @GetMapping("updateByPrimaryKey")
    public ResponseBean updateByPrimaryKey(User id) {
        int one = userService.updateByPrimaryKey(id);
        return ResponseBean.success(one);
    }
    
}
